
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Дерево каталогов</title>
	<link rel="stylesheet" href="public/css/style.css" media="all">
	<script type="text/javascript" src="public/js/jquery-1.8.3.min.js"></script>
	<script type="text/javascript">
	jQuery(document).ready(function(){
		// поиск файла или директории
		
		$('#s_button').click(function(){
			$('.detect').removeClass("detect");
			$.ajax({
				"dataType":'json',
				"type": "POST",
				"data": "search_data=" + $('#s_name').val(),
				"url": 'index.php',
				"success" : function (result){
					if(result){
						if(result.is_error == true){
							$('.error_msg').html(result.msg);
						} else {
							$('.error_msg').html('');
							for (var key in result.response) {
    							var val = result.response[key];
    							for(var key_in in val){
    								if(key_in == (val.length - 1)) { 
    									$('#' + val[key_in]).addClass("detect");
    								}
    								if( !$('#' + val[key_in]).hasClass("open") ){
    									click_link( $('#' + val[key_in]) , false);
    								} 
    							}
							}
						}
					}
				}
			})
		})
	
		// навигация по дереву
		$('.tree span.link').live("click", function(){
			click_link($(this));
		}) 

		function click_link(obj, async){
			var link = obj;
			if( link.hasClass("open") ) { 
				link.removeClass("open");
				$(".parent_id_" + link.attr('id')).empty();
				$("#plus_id_" +  link.attr('id')).html('+');
			} else {
				$.ajax({
					"async" : async,
					"dataType":'json',
					"type": "POST",
					"data": "parent_id=" + link.attr('id'),
					"url": 'index.php',
					"success" : function (result){
						if(result){
							link.addClass("open");
							$("#plus_id_" +  link.attr('id')).html('-');
							$(".parent_id_" + link.attr('id')).empty();
							for (var key in result) {
	    						var val = result[key];
	    						// собираем html
	    						var htmlIn = "<li><span id='" + val['id'] + "' class='link'>" ;
	    						if(val['type'] == 'direct') { 
	    							if(val['count_in'] > 0){
	    								htmlIn += "<span id='plus_id_" + val['id'] + "'' class='plus'>+</span>";
	    							} else {
	    								htmlIn += "<span id='plus_id_" + val['id'] + "'' class='plus'>-</span>";
	    							}
									htmlIn += "<span class='dir_pict'>&nbsp&nbsp&nbsp&nbsp</span>";
								} else {
									htmlIn += "<span class='file_pict'>&nbsp&nbsp&nbsp&nbsp</span>";
								}
	    						htmlIn += val['name'] + "</span>";
	    						if(val['type'] == 'file') { htmlIn += "<span class='size'>размер: " + val['size'] + " B </span>"} else {
	    							htmlIn += "<ul class='parent_id_" + val['id'] + "'></ul></li>";	
	    						}
	    						//----------------//
	    						$('.parent_id_' + link.attr('id')).append(htmlIn);
							}
						}
					}
				});
			}
		}

	});
	</script>
</head>
<body>
	<div class="left_side">
		<div>
			<span class="title_menu">Экспортировать в файл</span>
			<form action="index.php" method="POST" name="f_export">
				В браузере
				<input type="radio" name="select_out" id="select_out" value="window" checked>
				Скачать файл
				<input type="radio" name="select_out" id="select_out" value="file">
				<select name="type_exp" id="type_exp_id">
					<option value="xml">XML</option>
					<option value="csv">CSV</option>
					<option value="json">JSON</option>
				</select>
				<input type="submit" value="Экспорт"/>		
			</form>
		</div>
		<div class="search_block">
			<span class="title_menu">Поиск директории или файла</span>
			<input type="text" name="search" id="s_name">
			<input type="button" value="Найти" id="s_button">
			<div class="error_msg"></div>
		</div>	
	</div>
	<div class="prim_window">
		<? if(isset($root)){ ?>
			<div class="root_name"><?=$root['full_path']?></div>
		<?}?>
		<? if(isset($data)){ ?>
			<ul class="tree">
				<? foreach ($data as $key => $value) { ?>
					<li>
						<span id='<?=$value['id']?>' class="link">

						<?if( $value['type'] == 'direct') {?>
							<?if( $value['count_in'] > 0){?>
								<span id="plus_id_<?=$value['id']?>" class="plus">+</span>
							<?} else {?>
								<span id="plus_id_<?=$value['id']?>" class="plus">-</span>
							<?}?>
							<span id="pict_id_<?=$value['id']?>" class="dir_pict">&nbsp&nbsp&nbsp&nbsp</span>
						<?} else {?>
							<span id="pict_id_<?=$value['id']?>" class="file_pict">&nbsp&nbsp&nbsp&nbsp</span>
						<?}?>
						<?=$value['name']?></span>
						<ul class="parent_id_<?=$value['id']?>"></ul>
					</li>
				<?}?>
			</ul>
		<?}?>
	</div>
</body>
</html>